﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace JavaDevToys.Rcl.Utils
{
    public sealed class SqlServerHelp
    {
        public static string BuildConnectionString(
            string server,
            string database,
            string username = null,
            string password = null,
            string port = "1433",
            bool useWindowsAuthentication = false,
            int timeout = 300)
        {
            StringBuilder connectionString = new StringBuilder();

            // Server information
            connectionString.Append("Server=");
            connectionString.Append(server);

            if (!string.IsNullOrEmpty(port))
            {
                connectionString.Append(",");
                connectionString.Append(port);
            }

            connectionString.Append(";");

            // Database name
            connectionString.Append("Database=");
            connectionString.Append(database);
            connectionString.Append(";");

            // Authentication
            if (useWindowsAuthentication)
            {
                connectionString.Append("Integrated Security=True;");
            }
            else
            {
                if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(password))
                {
                    throw new ArgumentException("Username and password are required for SQL Server authentication.");
                }
                connectionString.Append("User Id=");
                connectionString.Append(username);
                connectionString.Append(";Password=");
                connectionString.Append(password);
                connectionString.Append(";");
            }

            // Connection timeout
            connectionString.Append("Connection Timeout=");
            connectionString.Append(timeout);
            connectionString.Append(";");
            connectionString.Append("TrustServerCertificate=true;");

            return connectionString.ToString();
        }
    }
}
